﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TinyCinema.Data
{
    public class BookedTicket:Execution
    {
        public string ID { get; set; }
        public Customer Customer { get; set; }
        public ObservableCollection<Ticket> Tickets { get; set; }
        public decimal Value { get; set; }
        public DateTime BookedDate { get; set; }
        public bool IsCanceled { get; set; }
        public bool IsPurchased { get; set; }
        public Schedule Schedule { get; set; }
        public ObservableCollection<Desk> lstDesk { get; set; }
        public BookedTicket(Customer customer, Schedule schedule, ObservableCollection<Desk> lstDesk)
        {
            ID = IDGenerate(Tables.BookedTicket);
            BookedDate = DateTime.Now;
            Customer = customer;
            Tickets = new ObservableCollection<Ticket>();
            Value = schedule.TypePrice.Price;
            Data.Promotion CurrentPromotion = null;
            var lstPromotion = Data.Promotion.GetAllPromotion();
            foreach (var promotion in lstPromotion)
            {
                if (promotion.StartDate <= DateTime.Now && promotion.EndDate >= DateTime.Now)
                {
                    CurrentPromotion = promotion;
                    break;
                }
            }
            foreach (var Desk in lstDesk)
            {
                Value += Desk.Type.AdditionalPrice;                
                decimal TicketPrice = schedule.TypePrice.Price + Desk.Type.AdditionalPrice;
                Tickets.Add(new Ticket(schedule, Desk, TicketPrice));
            }
            Value = decimal.Parse((double.Parse(Value.ToString()) * Customer.Type.Discount).ToString());
            if (CurrentPromotion != null)
                foreach (var CusType in CurrentPromotion.CustomerTypes)
                    if (CusType.ID == Customer.Type.ID)
                        Value = decimal.Parse((double.Parse(Value.ToString()) * CusType.PromotionDiscount).ToString());
            Save();
        }
        public BookedTicket()
        {
            lstDesk = new ObservableCollection<Desk>();
        }
        public BookedTicket(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.BookedTicket);
            using (var tbl = ds.Tables[Tables.BookedTicket])
            {
                var Row = tbl.Select(BookedTicketTable.ID + "='" + ID + "'")[0];
                Customer = new Customer(Row[BookedTicketTable.CustomerID] as string);
                Value = (decimal)Row[BookedTicketTable.Value];
                BookedDate = (DateTime)Row[BookedTicketTable.BookedDate];
            }
            lstDesk = new ObservableCollection<Desk>();
            PutTableIntoDataSet(Tables.Ticket);
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                Tickets = new ObservableCollection<Ticket>();
                var Rows = tbl.Select(TicketTable.BookedTicketID + "='" + ID + "'");
                if (Rows.Length == 0) IsCanceled = true;
                foreach(var Row in Rows)
                {
                    Tickets.Add(new Ticket(Row[TicketTable.ID] as string));
                    lstDesk.Add(new Desk(Row[TicketTable.DeskID] as string));
                    if (Row[TicketTable.TicketOrder] != System.DBNull.Value) IsPurchased = true;
                }
                if (Rows.Length != 0)  Schedule = Tickets[0].Schedule;
            }
            
        }
        public void Delete()
        {
            //Kiểm tra đã thanh toán hay chưa
            PutTableIntoDataSet(Tables.Ticket);
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                var Rows = tbl.Select(TicketTable.BookedTicketID + "='" + ID + "'");
                if (Rows.Length == 0) IsCanceled = true;
                else
                foreach (var Row in Rows)
                    if (Row[TicketTable.TicketOrder] != System.DBNull.Value)
                    {
                        IsPurchased = true;
                        break;
                    }             
            }
            if (IsPurchased || IsCanceled) return;
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                var Rows = tbl.Select(TicketTable.BookedTicketID + "='" + ID + "'");
                foreach (var Row in Rows) Row.Delete();
                IsCanceled = true;
                SaveCommand(Tables.Ticket);
            }
        }
        public void Save()
        {
            PutTableIntoDataSet(Tables.BookedTicket);
            using (var tbl = ds.Tables[Tables.BookedTicket])
            {
                var Row = tbl.NewRow();
                Row[BookedTicketTable.ID] = ID;
                Row[BookedTicketTable.BookedDate] = BookedDate;
                Row[BookedTicketTable.Value] = Value;
                Row[BookedTicketTable.CustomerID] = Customer.ID;
                tbl.Rows.Add(Row);
                SaveCommand(Tables.BookedTicket);
            }

            //Lưu vé
            PutTableIntoDataSet(Tables.Ticket);
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                foreach (var Ticket in Tickets)
                {
                    var Rows = tbl.Select(TicketTable.ID + "='" + Ticket.ID + "'");
                    if (Rows.Length == 0)
                    {
                        var Row = tbl.NewRow();
                        Row[TicketTable.ID] = Ticket.ID;
                        Row[TicketTable.ScheduleID] = Ticket.Schedule.ID;
                        Row[TicketTable.DeskID] = Ticket.Desk.ID;
                        Row[TicketTable.BookedTicketID] = ID;
                        Row[TicketTable.Price] = Ticket.Price;
                        tbl.Rows.Add(Row);
                    }
                    else
                    {
                        var Row = Rows[0];
                        Row[TicketTable.ID] = Ticket.ID;
                        Row[TicketTable.ScheduleID] = Ticket.Schedule.ID;
                        Row[TicketTable.DeskID] = Ticket.Desk.ID;
                        Row[TicketTable.BookedTicketID] = ID;
                        Row[TicketTable.Price] = Ticket.Price;
                    }
                }
                SaveCommand(Tables.Ticket);
            } 
        }
        public static ObservableCollection<BookedTicket> Filter(DateTime from, DateTime to, string CustomerID)
        {
            PutTableIntoDataSet(Tables.BookedTicket);
            var tbl = ds.Tables[Tables.BookedTicket];
            var Rows = tbl.Select(BookedTicketTable.BookedDate + " >= '" + from + "' and " + BookedTicketTable.BookedDate + " <= '" + to + "' and " + BookedTicketTable.CustomerID + "='" + CustomerID + "'");
            var Result = new ObservableCollection<BookedTicket>();
            foreach (var Row in Rows)
            {
                Result.Add(new BookedTicket(Row[BookedTicketTable.ID] as String));
            }
            return Result;
        }
    }
}
